#!/usr/bin/env ruby
# RedBook is a command-line personal logger and time tracker written in Ruby.
#
# Web Site:: http://redbook.h3rald.com
# Author::    Fabio Cevasco (mailto:h3rald@h3rald.com)
# Copyright:: Copyright (c) 2007-2008 Fabio Cevasco
# License::   BSD
module RedBook
	MODE = 'exe' # source/gem/exe
end

## TODO: Error handling here...

require 'rubyscript2exe' if RedBook::MODE == 'exe'
require 'ftools'
require 'yaml'
require 'chronic'
require 'active_support'
require 'observer'
require 'rawline'
require 'iconv'

if PLATFORM.match(/win32/i) then
	begin
		require 'win32console'
		RedBook::COLORS_ENABLED = true
	rescue
		RedBook::COLORS_ENABLED = false
	end
else
		RedBook::COLORS_ENABLED = true
end

module RedBook
	if MODE == 'exe' then
		if RUBYSCRIPT2EXE.is_compiling? then
			HOME = File.dirname(File.expand_path(__FILE__))
			EXEHOME = HOME
		else
			HOME = RUBYSCRIPT2EXE.appdir.gsub('\\','/')
			EXEHOME = RUBYSCRIPT2EXE.exedir.gsub('\\','/')
		end
	else
		HOME = File.dirname(File.expand_path(__FILE__))
		EXEHOME = HOME
	end

	VERSION = "1.0.0"
	NAME = "RedBook"
	WELCOME = "#{NAME} v#{VERSION} - Copyright (c) 2007-#{Time.now.strftime("%Y")}, Fabio Cevasco"
	
	@@rawline_editor = RawLine::Editor.new

	def self.rawline_editor
		@@rawline_editor
	end
	
	def self.define_shortcut(seq, action)
		write = lambda { |c| @@rawline_editor.write_line(c) }
		@@rawline_editor.bind({seq => "\e#{seq.to_s}" }) { write.call(action)}
	end
end

require File.exist?("#{ENV['HOME']}/rbinit.rb") ? "#{ENV['HOME']}/rbinit.rb" : "#{RedBook::EXEHOME}/rbinit.rb"

require "#{RedBook::HOME}/redbook/core_extensions.rb"
require "#{RedBook::HOME}/redbook/utils.rb"
require "#{RedBook::HOME}/redbook/config.rb"
require "#{RedBook::HOME}/redbook/hook_collection.rb"
require "#{RedBook::HOME}/redbook/timecalc.rb"
require "#{RedBook::HOME}/parsers/parser.rb"
require "#{RedBook::HOME}/redbook/logger.rb"


## Interface
interface_dir = (RedBook::INTERFACE_DIR.blank?) ? "#{RedBook::HOME}/interfaces/" : RedBook::INTERFACE_DIR
if RedBook::INTERFACE == 'console' then
	require "#{RedBook::HOME}/interfaces/interface.rb"
else
	interface = "#{interface_dir}#{RedBook::INTERFACE}_interface.rb" 
	if File.exists? interface then
		require interface
		break
	end
end

# Plugins
loaded_plugins = []
plugin_dirs = ["#{RedBook::HOME}/plugins/"]
plugin_dirs.merge! RedBook::PLUGIN_DIRS unless RedBook::PLUGIN_DIRS.blank?
RedBook::PLUGINS.each do |p|
	plugin_dirs.each do |d|
		plugin = "#{d}#{p}_plugin.rb"
		if File.exists? plugin then
			begin
				require plugin
				loaded_plugins << p 
				break
			rescue Exception
				next
			end
		end
	end
end
RedBook::LOADED_PLUGINS = loaded_plugins


## Renderers
loaded_renderers = []
renderer_dirs = ["#{RedBook::HOME}/renderers/"]
renderer_dirs.merge! RedBook::RENDERER_DIRS unless RedBook::RENDERER_DIRS.blank?
RedBook::RENDERERS.each do |r|
	if r == 'console' then
		require "#{RedBook::HOME}/renderers/renderer.rb"
		loaded_renderers << r 
	else
		renderer_dirs.each do |d|
			renderer = "#{d}#{r}_renderer.rb" 
			if File.exists? renderer then
				begin
					require renderer
					loaded_renderers << r 
					break
				rescue Exception
					next
				end
			end
		end
	end
end
RedBook::LOADED_RENDERERS = loaded_renderers


